package com.yxhl.zoume.core.tripsmgmt.presenter.unit;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.ICommentPresenter;
import com.yxhl.zoume.core.tripsmgmt.view.unit.CommentOnDriverView;
import com.yxhl.zoume.data.http.rest.param.tripsmgnt.CommentDriverParam;
import com.yxhl.zoume.data.http.rest.response.tirps.CommentDriverResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.CommentDriverUseCase;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/5/25.
 */
public class CommentOnDriverPresenter extends BasePresenter implements IPresenter, ICommentPresenter {

    @Inject
    CommentDriverUseCase mCommentDriverUseCase;

    private CommentOnDriverView mCommentOnDriverView;

    @Inject
    public CommentOnDriverPresenter(Context context) {
        super(context);
    }

    @Override
    public void commentOnDriver(String orderId, int starAmount, String commentMessage) {
        final CommentDriverParam commentDriverParam = new CommentDriverParam(
                getMobile(),
                getToken(),
                orderId,
                starAmount,
                commentMessage
        );

        mCommentDriverUseCase.execute(commentDriverParam, new ZMSubscriber<CommentDriverResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                mCommentOnDriverView.closeCommentOnCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(CommentDriverResponse commentDriverResponse) {
                super.onNext(commentDriverResponse);
                boolean succ = commentDriverResponse.isSucc();
                String resultMessage = commentDriverResponse.getResultMessage();
                if (succ) {
                    mCommentOnDriverView.renderCommentOnSuccess();
                } else {
                    mCommentOnDriverView.renderCommentOnError(resultMessage);
                }

            }
        });

    }


    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        mCommentDriverUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof CommentOnDriverView) {
            mCommentOnDriverView = (CommentOnDriverView) view;
        }
    }


}
